package com.bwie.es.sync;

import com.bwie.common.domain.response.GoodsQueryRes;
import com.bwie.common.result.Result;
import com.bwie.es.remote.GoodsRemoteService;
import com.bwie.es.service.GoodsEsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
@Log4j2
public class SyncGoodsEs {

    @Autowired
    private GoodsRemoteService goodsRemoteService;
    @Autowired
    private GoodsEsService goodsEsService;

    @Scheduled(cron = "0 */1 * * * ?")
    public void syncGoodsEs(){
        Result<List<GoodsQueryRes>> all = goodsRemoteService.findAll();
        List<GoodsQueryRes> data = all.getData();
        if (!CollectionUtils.isEmpty(data)){
            goodsEsService.deleteAll();
            goodsEsService.batchAdd(data);
        }
    }
}
